#include <string>
#include <vector>
#include <iostream>
#include <ani.h>
#include <array>
#include <cstring>

using TextDecorationType = int32_t;
using TextDecorationStyle = int32_t;
using TextBaseline = int32_t;
using FontWeight = int32_t;
using FontStyle = int32_t;
using EllipsisMode = int32_t;

ani_ref g_textStyleClass {};
ani_ref g_decorationClass {};
ani_ref g_colorClass {};
ani_ref g_rectStyleClass {};

ani_method g_textStyle_getter_decoration {};
ani_method g_textStyle_getter_color {};
ani_method g_textStyle_getter_baseline {};
ani_method g_textStyle_getter_frontWeight {};
ani_method g_textStyle_getter_fontStyle {};
ani_method g_textStyle_getter_fontFamilies {};
ani_method g_textStyle_getter_fontSize {};
ani_method g_textStyle_getter_letterSpacing {};
ani_method g_textStyle_getter_wordSpacing {};
ani_method g_textStyle_getter_heightScale {};
ani_method g_textStyle_getter_halfLeading {};
ani_method g_textStyle_getter_heightOnly {};
ani_method g_textStyle_getter_ellipsis {};
ani_method g_textStyle_getter_ellipsisMode {};
ani_method g_textStyle_getter_locale {};
ani_method g_textStyle_getter_baselineShift {};
ani_method g_textStyle_getter_fontFeatures {};
ani_method g_textStyle_getter_frontVariations {};
ani_method g_textStyle_getter_backgroundRect {};

ani_method g_decorationClass_getter_textDecoration {};
ani_method g_decorationClass_getter_color {};
ani_method g_decorationClass_getter_decorationStyle {};
ani_method g_decorationClass_getter_decorationThickness {};

ani_method g_colorClass_getter_alpha {};
ani_method g_colorClass_getter_red {};
ani_method g_colorClass_getter_green {};
ani_method g_colorClass_getter_blue {};

ani_method g_rectStyleClass_getter_leftTopRadius {};
ani_method g_rectStyleClass_getter_rightTopRadius {};
ani_method g_rectStyleClass_getter_rightBottomRadius {};
ani_method g_rectStyleClass_getter_leftBottomRadius {};

void cacheTextStyleClass(ani_env *env) {
    ani_class textStyleClass;
    auto status = env->FindClass("ani_serialize.TextStyle", &textStyleClass);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle class, ERROR: "<< status << std::endl;
        return;
    }
    status = env->GlobalReference_Create(static_cast<ani_ref>(textStyleClass), &g_textStyleClass);
    if (status != ANI_OK) {
        std::cerr << "Failed to create global reference for TextStyle class, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>decoration", nullptr, &g_textStyle_getter_decoration);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_decoration method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>color", nullptr, &g_textStyle_getter_color);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_color method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>baseline", nullptr, &g_textStyle_getter_baseline);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_baseline method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>frontWeight", nullptr, &g_textStyle_getter_frontWeight);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_frontWeight method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>fontStyle", nullptr, &g_textStyle_getter_fontStyle);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_fontStyle method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>fontFamilies", nullptr, &g_textStyle_getter_fontFamilies);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_fontFamilies method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>fontSize", nullptr, &g_textStyle_getter_fontSize);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_fontSize method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>letterSpacing", nullptr, &g_textStyle_getter_letterSpacing);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_letterSpacing method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>wordSpacing", nullptr, &g_textStyle_getter_wordSpacing);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_wordSpacing method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>heightScale", nullptr, &g_textStyle_getter_heightScale);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_heightScale method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>halfLeading", nullptr, &g_textStyle_getter_halfLeading);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_halfLeading method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>heightOnly", nullptr, &g_textStyle_getter_heightOnly);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_heightOnly method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>ellipsis", nullptr, &g_textStyle_getter_ellipsis);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_ellipsis method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>ellipsisMode", nullptr, &g_textStyle_getter_ellipsisMode);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_ellipsisMode method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>locale", nullptr, &g_textStyle_getter_locale);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_locale method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>baselineShift", nullptr, &g_textStyle_getter_baselineShift);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_baselineShift method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>fontFeatures", nullptr, &g_textStyle_getter_fontFeatures);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_fontFeatures method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>frontVariations", nullptr, &g_textStyle_getter_frontVariations);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_frontVariations method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_textStyleClass), "<get>backgroundRect", nullptr, &g_textStyle_getter_backgroundRect);
    if (status != ANI_OK) {
        std::cerr << "Failed to find TextStyle.getter_backgroundRect method, ERROR: "<< status << std::endl;
        return;
    }

}

void cacheDecorationClass(ani_env *env){
    ani_class decorationClass;
    auto status = env->FindClass("ani_serialize.Decoration", &decorationClass);
    if (status != ANI_OK) {
        std::cerr << "Failed to find Decoration class, ERROR: "<< status << std::endl;
        return;
    }
    status = env->GlobalReference_Create(static_cast<ani_ref>(decorationClass), &g_decorationClass);
    if (status != ANI_OK) {
        std::cerr << "Failed to create global reference for Decoration class, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_decorationClass), "<get>textDecoration", nullptr, &g_decorationClass_getter_textDecoration);
    if (status != ANI_OK) {
        std::cerr << "Failed to find Decoration.getter_textDecoration method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_decorationClass), "<get>color", nullptr, &g_decorationClass_getter_color);
    if (status != ANI_OK) {
        std::cerr << "Failed to find Decoration.getter_color method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_decorationClass), "<get>decorationStyle", nullptr, &g_decorationClass_getter_decorationStyle);
    if (status != ANI_OK) {
        std::cerr << "Failed to find Decoration.getter_decorationStyle method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_decorationClass), "<get>decorationThickness", nullptr, &g_decorationClass_getter_decorationThickness);
    if (status != ANI_OK) {
        std::cerr << "Failed to find Decoration.getter_decorationThickness method, ERROR: "<< status << std::endl;
        return;
    }
}

void cacheColorClass(ani_env *env){
    ani_class colorClass;
    auto status = env->FindClass("ani_serialize.Color", &colorClass);
    if (status != ANI_OK) {
        std::cerr << "Failed to find Color class, ERROR: "<< status << std::endl;
        return;
    }
    status = env->GlobalReference_Create(static_cast<ani_ref>(colorClass), &g_colorClass);
    if (status != ANI_OK) {
        std::cerr << "Failed to create global reference for Color class, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_colorClass), "<get>alpha", nullptr, &g_colorClass_getter_alpha);
    if (status != ANI_OK) {
        std::cerr << "Failed to find Color.getter_alpha method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_colorClass), "<get>red", nullptr, &g_colorClass_getter_red);
    if (status != ANI_OK) {
        std::cerr << "Failed to find Color.getter_red method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_colorClass), "<get>green", nullptr, &g_colorClass_getter_green);
    if (status != ANI_OK) {
        std::cerr << "Failed to find Color.getter_green method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_colorClass), "<get>blue", nullptr, &g_colorClass_getter_blue);
    if (status != ANI_OK) {
        std::cerr << "Failed to find Color.getter_blue method, ERROR: "<< status << std::endl;
        return;
    }
}

void cacheRectStyleClass(ani_env *env){
    ani_class rectStyleClass;
    auto status = env->FindClass("ani_serialize.RectStyle", &rectStyleClass);
    if (status != ANI_OK) {
        std::cerr << "Failed to find RectStyle class, ERROR: "<< status << std::endl;
        return;
    }
    status = env->GlobalReference_Create(static_cast<ani_ref>(rectStyleClass), &g_rectStyleClass);
    if (status != ANI_OK) {
        std::cerr << "Failed to create global reference for RectStyle class, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_rectStyleClass), "<get>leftTopRadius", nullptr, &g_rectStyleClass_getter_leftTopRadius);
    if (status != ANI_OK) {
        std::cerr << "Failed to find RectStyle.getter_leftTopRadius method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_rectStyleClass), "<get>rightTopRadius", nullptr, &g_rectStyleClass_getter_rightTopRadius);
    if (status != ANI_OK) {
        std::cerr << "Failed to find RectStyle.getter_rightTopRadius method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_rectStyleClass), "<get>rightBottomRadius", nullptr, &g_rectStyleClass_getter_rightBottomRadius);
    if (status != ANI_OK) {
        std::cerr << "Failed to find RectStyle.getter_rightBottomRadius method, ERROR: "<< status << std::endl;
        return;
    }
    status = env->Class_FindMethod(static_cast<ani_class>(g_rectStyleClass), "<get>leftBottomRadius", nullptr, &g_rectStyleClass_getter_leftBottomRadius);
    if (status != ANI_OK) {
        std::cerr << "Failed to find RectStyle.getter_leftBottomRadius method, ERROR: "<< status << std::endl;
        return;
    }
}

void cacheClasses(ani_env *env) {
    cacheTextStyleClass(env);
    cacheDecorationClass(env);
    cacheColorClass(env);
    cacheRectStyleClass(env);
}

struct Color {
    int alpha;
    int red;
    int green;
    int blue;
};

struct Point {
    double x;
    double y;
};

struct FrontVariation {
    std::string axis;
    double value;
};

struct FontFeature {
    std::string name;
    int value;
};

struct RectStyle {
    double leftTopRadius;
    double rightTopRadius;
    double rightBottomRadius;
    double leftBottomRadius;
};

struct Decoration {
    TextDecorationType textDecoration;
    Color color;
    TextDecorationStyle decorationStyle;
    double decorationThickness;
};

struct TextStyle {
    Decoration decoration;
    Color color;
    TextBaseline baseline;
    FontWeight frontWeight;
    FontStyle fontStyle;
    std::vector<std::string> fontFamilies;
    double fontSize;
    double letterSpacing;
    double wordSpacing;
    double heightScale;
    bool halfLeading;
    bool heightOnly;
    std::string ellipsis;
    EllipsisMode ellipsisMode;
    std::string locale;
    double baselineShift;
    std::vector<FontFeature> fontFeatures;
    std::vector<FrontVariation> frontVariations;
    RectStyle backgroundRect;
};

template <typename T>
T read_primitive(const char *&data, ani_size &remaining_size) {
    if (remaining_size < sizeof(T)) {
        throw std::runtime_error("Buffer underflow when reading primitive.");
    }
    T value;
    std::memcpy(&value, data, sizeof(T));
    data += sizeof(T);
    remaining_size -= sizeof(T);
    return value;
}

void pushStyleNativeUnserialized([[maybe_unused]] ani_env *env, [[maybe_unused]] ani_object textStyle)
{
    TextStyle style = {};
    ani_object decorationObj;
    ani_object colorObj;
    ani_object rectStyleObj;
    ani_status status = env->Object_CallMethod_Ref(textStyle, g_textStyle_getter_decoration, reinterpret_cast<ani_ref*>(&decorationObj));
    if (status != ANI_OK) {
        std::cerr << "Failed to get decoration object from TextStyle" << std::endl;
        return;
    }
    status = env->Object_CallMethod_Ref(textStyle, g_textStyle_getter_color, reinterpret_cast<ani_ref*>(&colorObj));
    if (status != ANI_OK) {
        std::cerr << "Failed to get color object from TextStyle" << std::endl;
        return;
    }
    status = env->Object_CallMethod_Ref(textStyle, g_textStyle_getter_backgroundRect, reinterpret_cast<ani_ref*>(&rectStyleObj));
    if (status != ANI_OK) {
        std::cerr << "Failed to get backgroundRect object from TextStyle" << std::endl;
        return;
    }
    // 1. Decoration struct
    ani_ref textDecorationRef {};
    status = env->Object_CallMethod_Ref(decorationObj, g_decorationClass_getter_textDecoration, &textDecorationRef);
    if (status != ANI_OK) {
        std::cerr << "Failed to get textDecoration from Decoration" << std::endl;
        return;
    }
    ani_int textDecorationValue {};
    status = env->EnumItem_GetValue_Int(reinterpret_cast<ani_enum_item>(textDecorationRef), &textDecorationValue);
    if (status != ANI_OK) {
        std::cerr << "Failed to get int value from textDecoration enum" << std::endl;
        return;
    }
    style.decoration.textDecoration = static_cast<TextDecorationType>(textDecorationValue);
    ani_object decorationColorObj;
    status = env->Object_CallMethod_Ref(decorationObj, g_decorationClass_getter_color, reinterpret_cast<ani_ref*>(&decorationColorObj));
    if (status != ANI_OK) {
        std::cerr << "Failed to get color object from Decoration" << std::endl;
        return;
    }
    ani_int alphaValue {};
    ani_int redValue {};
    ani_int greenValue {};
    ani_int blueValue {};

    status = env->Object_CallMethod_Int(decorationColorObj, g_colorClass_getter_alpha, &alphaValue);
    if (status != ANI_OK) {
        std::cerr << "Failed to get alpha from Color" << std::endl;
        return;
    }
    status = env->Object_CallMethod_Int(decorationColorObj, g_colorClass_getter_red, &redValue);
    if (status != ANI_OK) {
        std::cerr << "Failed to get red from Color" << std::endl;
        return;
    }
    status = env->Object_CallMethod_Int(decorationColorObj, g_colorClass_getter_green, &greenValue);
    if (status != ANI_OK) {
        std::cerr << "Failed to get green from Color" << std::endl;
        return;
    }
    status = env->Object_CallMethod_Int(decorationColorObj, g_colorClass_getter_blue, &blueValue);
    if (status != ANI_OK) {
        std::cerr << "Failed to get blue from Color" << std::endl;
        return;
    }
    style.decoration.color = Color{255, static_cast<int>(redValue), static_cast<int>(greenValue), static_cast<int>(blueValue)};
    ani_ref decorationStyleRef {};
    status = env->Object_CallMethod_Ref(decorationObj, g_decorationClass_getter_decorationStyle, &decorationStyleRef);
    if (status != ANI_OK) {
        std::cerr << "Failed to get decorationStyle from Decoration" << std::endl;
        return;
    }
    ani_int decorationStyleValue {};
    status = env->EnumItem_GetValue_Int(reinterpret_cast<ani_enum_item>(decorationStyleRef), &decorationStyleValue);
    if (status != ANI_OK) {
        std::cerr << "Failed to get int value from decorationStyle enum" << std::endl;
        return;
    }
    style.decoration.decorationStyle = static_cast<TextDecorationStyle>(decorationStyleValue);
    status = env->Object_CallMethod_Double(decorationObj, g_decorationClass_getter_decorationThickness, &style.decoration.decorationThickness);
    if (status != ANI_OK) {
        std::cerr << "Failed to get decorationThickness from Decoration" << std::endl;
        return;
    }
    // 2. Color struct
    status = env->Object_CallMethod_Int(colorObj, g_colorClass_getter_alpha, &alphaValue);
    if (status != ANI_OK) {
        std::cerr << "Failed to get alpha from Color" << std::endl;
        return;
    }
    status = env->Object_CallMethod_Int(colorObj, g_colorClass_getter_red, &redValue);
    if (status != ANI_OK) {
        std::cerr << "Failed to get red from Color" << std::endl;
        return;
    }
    status = env->Object_CallMethod_Int(colorObj, g_colorClass_getter_green, &greenValue);
    if (status != ANI_OK) {
        std::cerr << "Failed to get green from Color" << std::endl;
        return;
    }
    status = env->Object_CallMethod_Int(colorObj, g_colorClass_getter_blue, &blueValue);
    if (status != ANI_OK) {
        std::cerr << "Failed to get blue from Color" << std::endl;
        return;
    }
    style.color = Color{static_cast<int>(alphaValue), static_cast<int>(redValue), static_cast<int>(greenValue), static_cast<int>(blueValue)};
    // 3. baseline
    ani_ref baselineRef {};
    status = env->Object_CallMethod_Ref(textStyle, g_textStyle_getter_baseline, &baselineRef);
    if (status != ANI_OK) {
        std::cerr << "Failed to get baseline from TextStyle" << std::endl;
        return;
    }
    ani_int baselineValue {};
    status = env->EnumItem_GetValue_Int(reinterpret_cast<ani_enum_item>(baselineRef), &baselineValue);
    if (status != ANI_OK) {
        std::cerr << "Failed to get int value from baseline enum" << std::endl;
        return;
    }
    style.baseline = static_cast<TextBaseline>(baselineValue);
    // 4. frontWeight
    ani_ref frontWeightRef {};
    status = env->Object_CallMethod_Ref(textStyle, g_textStyle_getter_frontWeight, &frontWeightRef);
    if (status != ANI_OK) { 
        std::cerr << "Failed to get frontWeight from TextStyle" << std::endl;
        return;
    }
    ani_int frontWeightValue {};
    status = env->EnumItem_GetValue_Int(reinterpret_cast<ani_enum_item>(frontWeightRef), &frontWeightValue);
    if (status != ANI_OK) {
        std::cerr << "Failed to get int value from frontWeight enum" << std::endl;
        return;
    }
    style.frontWeight = static_cast<FontWeight>(frontWeightValue);
    // 5. fontStyle
    ani_ref fontStyleRef {};
    status = env->Object_CallMethod_Ref(textStyle, g_textStyle_getter_fontStyle, &fontStyleRef);
    if (status != ANI_OK) {
        std::cerr << "Failed to get fontStyle from TextStyle" << std::endl;
        return;
    }
    ani_int fontStyleValue {};
    status = env->EnumItem_GetValue_Int(reinterpret_cast<ani_enum_item>(fontStyleRef), &fontStyleValue);
    if (status != ANI_OK) {
        std::cerr << "Failed to get int value from fontStyle enum" << std::endl;
        return;
    }
    style.fontStyle = static_cast<FontStyle>(fontStyleValue);
    // 6. fontFamilies - 忽略
    // 7. fontSize
    status = env->Object_CallMethod_Double(textStyle, g_textStyle_getter_fontSize, &style.fontSize);
    if (status != ANI_OK) {
        std::cerr << "Failed to get fontSize from TextStyle" << std::endl;
        return;
    }
    // 8. letterSpacing
    status = env->Object_CallMethod_Double(textStyle, g_textStyle_getter_letterSpacing, &style.letterSpacing);
    if (status != ANI_OK) {
        std::cerr << "Failed to get letterSpacing from TextStyle" << std::endl;
        return;
    }
    // 9. wordSpacing
    status = env->Object_CallMethod_Double(textStyle, g_textStyle_getter_wordSpacing, &style.wordSpacing);
    if (status != ANI_OK) {
        std::cerr << "Failed to get wordSpacing from TextStyle" << std::endl;
        return;
    }
    // 10. heightScale
    status = env->Object_CallMethod_Double(textStyle, g_textStyle_getter_heightScale, &style.heightScale);
    if (status != ANI_OK) {
        std::cerr << "Failed to get heightScale from TextStyle" << std::endl;
        return;
    }
    // 11. halfLeading
    ani_boolean halfLeadingValue {};
    status = env->Object_CallMethod_Boolean(textStyle, g_textStyle_getter_halfLeading, &halfLeadingValue);
    if (status != ANI_OK) {
        std::cerr << "Failed to get halfLeading from TextStyle" << std::endl;
        return;
    }
    style.halfLeading = static_cast<bool>(halfLeadingValue);
    // 12. heightOnly
    ani_boolean heightOnlyValue {};
    status = env->Object_CallMethod_Boolean(textStyle, g_textStyle_getter_heightOnly, &heightOnlyValue);
    if (status != ANI_OK) {
        std::cerr << "Failed to get heightOnly from TextStyle" << std::endl;
        return;
    };
    style.heightOnly = static_cast<bool>(heightOnlyValue);
    // 13. ellipsis - 忽略
    // 14. ellipsisMode
    ani_ref ellipsisModeRef {};
    status = env->Object_CallMethod_Ref(textStyle, g_textStyle_getter_ellipsisMode, &ellipsisModeRef);
    if (status != ANI_OK) {
        std::cerr << "Failed to get ellipsisMode from TextStyle" << std::endl;
        return;
    }
    ani_int ellipsisModeValue {};
    status = env->EnumItem_GetValue_Int(reinterpret_cast<ani_enum_item>(ellipsisModeRef), &ellipsisModeValue);
    if (status != ANI_OK) {
        std::cerr << "Failed to get int value from ellipsisMode enum" << std::endl;
        return;
    }
    style.ellipsisMode = static_cast<EllipsisMode>(ellipsisModeValue);
    // 15. locale - 忽略
    // 16. baselineShift
    status = env->Object_CallMethod_Double(textStyle, g_textStyle_getter_baselineShift, &style.baselineShift);
    if (status != ANI_OK) {
        std::cerr << "Failed to get baselineShift from TextStyle" << std::endl;
        return;
    }
    // 17. fontFeatures - 忽略
    // 18. frontVariations - 忽略
    // 19. backgroundRect
    status = env->Object_CallMethod_Double(rectStyleObj, g_rectStyleClass_getter_leftTopRadius, &style.backgroundRect.leftTopRadius);
    if (status != ANI_OK) {
        std::cerr << "Failed to get leftTopRadius from RectStyle" << std::endl;
        return;
    }
    status = env->Object_CallMethod_Double(rectStyleObj, g_rectStyleClass_getter_rightTopRadius, &style.backgroundRect.rightTopRadius);
    if (status != ANI_OK) {
        std::cerr << "Failed to get rightTopRadius from RectStyle" << std::endl;
        return;
    }
    status = env->Object_CallMethod_Double(rectStyleObj, g_rectStyleClass_getter_rightBottomRadius, &style.backgroundRect.rightBottomRadius);
    if (status != ANI_OK) {
        std::cerr << "Failed to get rightBottomRadius from RectStyle" << std::endl;
        return;
    }
    status = env->Object_CallMethod_Double(rectStyleObj, g_rectStyleClass_getter_leftBottomRadius, &style.backgroundRect.leftBottomRadius);
    if (status != ANI_OK) {
        std::cerr << "Failed to get leftBottomRadius from RectStyle" << std::endl;
        return;
    }

    // check testStyle content
    // std::cout << "Successfully parsed TextStyle object (Unserialized)." << std::endl;
    // std::cout << "Font Size: " << style.fontSize << std::endl;
    // std::cout << "Decoration Thickness: " << style.decoration.decorationThickness << std::endl;
}  

void pushStyleNativeSerialized(ani_env *env, ani_arraybuffer buffer)
{
    ani_size resultSize = 0;
    void *data;
    auto status = env->ArrayBuffer_GetInfo(buffer, &data, &resultSize);
    if (status != ANI_OK)
    {
        std::cerr << "Failed to get ArrayBuffer info" << std::endl;
        return;
    }
    const char *current_data = static_cast<const char *>(data);
    ani_size remaining_size = resultSize;

    TextStyle style = {};
    try {
        // 1. Decoration struct (全部是固定大小成员)
        style.decoration.textDecoration = read_primitive<TextDecorationType>(current_data, remaining_size);
        style.decoration.color          = read_primitive<Color>(current_data, remaining_size);
        style.decoration.decorationStyle = read_primitive<TextDecorationStyle>(current_data, remaining_size);
        style.decoration.decorationThickness = read_primitive<double>(current_data, remaining_size);

        // 2. TextStyle - 静态成员 (直到第一个动态成员 fontFamilies)
        style.color             = read_primitive<Color>(current_data, remaining_size);
        style.baseline          = read_primitive<TextBaseline>(current_data, remaining_size);
        style.frontWeight       = read_primitive<FontWeight>(current_data, remaining_size);
        style.fontStyle         = read_primitive<FontStyle>(current_data, remaining_size);
        
        // --- 3. 忽略 std::vector<std::string> fontFamilies ---

        // 4. TextStyle - 静态成员 (font size 到 halfLeading)
        style.fontSize          = read_primitive<double>(current_data, remaining_size);
        style.letterSpacing     = read_primitive<double>(current_data, remaining_size);
        style.wordSpacing       = read_primitive<double>(current_data, remaining_size);
        style.heightScale       = read_primitive<double>(current_data, remaining_size);
        style.halfLeading       = read_primitive<bool>(current_data, remaining_size);
        style.heightOnly        = read_primitive<bool>(current_data, remaining_size);

        // --- 5. 忽略 std::string ellipsis ---
        
        // 6. TextStyle - 静态成员 (ellipsisMode)
        style.ellipsisMode      = read_primitive<EllipsisMode>(current_data, remaining_size);

        // --- 7. 忽略 std::string locale ---

        // 8. TextStyle - 静态成员 (baselineShift)
        style.baselineShift     = read_primitive<double>(current_data, remaining_size);
        
        // --- 9. 忽略 std::vector<FontFeature> fontFeatures ---

        // --- 10. 忽略 std::vector<FrontVariation> frontVariations ---

        // 11. RectStyle (全部是固定大小成员)
        style.backgroundRect    = read_primitive<RectStyle>(current_data, remaining_size);

        // 此时，所有固定大小的字段已从 ArrayBuffer 正确解析到 style 对象
        // std::cout << "Successfully parsed fixed-size TextStyle fields (Little-Endian)." << std::endl;
        // std::cout << "Font Size: " << style.fontSize << std::endl;
        // std::cout << "Decoration Thickness: " << style.decoration.decorationThickness << std::endl;
        
    } catch (const std::exception &e) {
        std::cerr << "Parsing error: " << e.what() << std::endl;
        return;
    }
}

ani_status ANI_Constructor(ani_vm *vm, uint32_t *result)
{
    ani_env *env;
    auto status = vm->GetEnv(ANI_VERSION_1, &env);
    if (status != ANI_OK)
    {
        std::cerr << "Unsupported ANI_VERSION_1" << std::endl;
        return status;
    }

    static const char *moduleName = "ani_serialize";
    ani_module md;
    status = env->FindModule(moduleName, &md);
    if (status != ANI_OK)
    {
        std::cerr << "Not found '" << moduleName << "'" << std::endl;
        return status;
    }
    std::array methods = {
        ani_native_function{"pushStyleSerialized", nullptr, reinterpret_cast<void *>(pushStyleNativeSerialized)},
        ani_native_function{"pushStyleUnserialized", nullptr, reinterpret_cast<void *>(pushStyleNativeUnserialized)},
    };
    std::cout << "Start bind native methods to '" << moduleName << "'" << std::endl;
    status = env->Module_BindNativeFunctions(md, methods.data(), methods.size());
    if (status != ANI_OK)
    {
        std::cerr << "Cannot bind native methods to '" << moduleName << "'" << std::endl;
        return status;
    };
    std::cout << "Finish bind native methods to '" << moduleName << "'" << std::endl;
    cacheClasses(env);
    *result = ANI_VERSION_1;
    return ANI_OK;
}